<template>
  <div>
    <div class="lx-top-body">
      <div class="top-border" style="width: 14%">
        <div class="query-title">选择查询场地:</div>
        <div style="margin: 20px">
          <el-checkbox v-model="query_date" @change="changeQueryType()"
            >按时间查询</el-checkbox
          >
        </div>
        <div style="margin: 20px">
          <el-checkbox v-model="query_week" @change="changeQueryWeek()"
            >按周次查询</el-checkbox
          >
        </div>
      </div>
      <div class="top-border" style="width: 85%">
        <div class="query-title">请选择查询条件：</div>
        <div style="color: #004ca7; margin-top: 20px;margin-left: 100px;">
          <div class="query-date" v-show="query_date == true">
            <span>开始时间：<span class="red">*</span></span>
            <el-date-picker
              style="margin-left: 20px; width: 185px"
              v-model="startDdate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
            <span class="mar-lef-2">结束时间：<span class="red">*</span></span>
            <el-date-picker
              style="margin-left: 20px; width: 185px"
              v-model="endDate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </div>
          <div class="query-date" v-show="query_date == false">
            <span>学期：<span class="red">*</span></span>
            <el-select v-model="semester" placeholder="请选择" size="mini" clearable>
              <el-option
                v-for="(item, index) in semesterList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>

            <span class="mar-lef-2">开始周次：<span class="red">*</span></span>
            <el-select v-model="startWeek" placeholder="请选择" size="mini" clearable>
              <el-option
                v-for="(item, index) in weekList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>

            <span class="mar-lef-2">结束周次：<span class="red">*</span></span>
            <el-select v-model="endWeek" placeholder="请选择" size="mini" clearable>
              <el-option
                v-for="(item, index) in weekList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </div>
          <div style="display: flex; margin: 20px 0">
            <span>星期：</span>
            <el-checkbox-group v-model="queryWeekList">
              <el-checkbox label="1">星期一</el-checkbox>
              <el-checkbox label="2">星期二</el-checkbox>
              <el-checkbox label="3">星期三</el-checkbox>
              <el-checkbox label="4">星期四</el-checkbox>
              <el-checkbox label="5">星期五</el-checkbox>
              <el-checkbox label="6">星期六</el-checkbox>
              <el-checkbox label="7">星期日</el-checkbox>
            </el-checkbox-group>
          </div>
          <div style="display: flex">
            <span>开始节次：</span>
            <el-select v-model="startSection" placeholder="请选择" size="mini" clearable>
              <el-option
                v-for="(item, index) in sectionList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                
              >
              </el-option>
            </el-select>
            <span class="mar-lef-2">结束节次：</span>
            <el-select v-model="endSection" placeholder="请选择" size="mini" clearable>
              <el-option
                v-for="(item, index) in sectionList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>

            <div class="search-button" style="margin-left: 300px" @click="queryFreePlace">
              <img src="../../../../assets/assessment/icon-search.png" alt="" />
              <span>查询</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="assessment-container">
      <div class="table-container">
        <el-table
          :data="tableData"
          border
          class="table-data"
          row-class-name="row-class"
          cell-class-name="cell-class"
          header-row-class-name="header-row-class"
          header-cell-class-name="header-cell-class"
          style="height: calc(100vh - 500px); overflow-y: auto"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="80"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="cdmc"
            label="场地场所名称"
            align="center"
          >
          <template slot-scope="scope">
              <span class="undeLine" @click="toOpenPlace(scope.row)">{{scope.row['cdmc']}}</span>
          </template>
          </el-table-column>
          <el-table-column
            prop="mj"
            label="面积(平方米)"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="krnrs"
            label="容纳人数"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="cdlx"
            label="场地场所类型"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="ssxq"
            label="所属校区"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="ssjzw"
            label="所属建筑物"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="dept"
            label="管理部门"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="floor"
            label="楼层"
            align="center"
          ></el-table-column>
        </el-table>
      </div>
    </div>
    <div class="wrapper-container">
      <div class="bottom-block" style="display: block">
        <div class="export-button" @click="exportData()">
          <img src="../../../../assets/assessment/icon-export.png" alt="" />
          <span>&ensp;导出Excel</span>
        </div>

        <div class="text">当前共找到{{ total }}条记录,当前显示{{ startPage }}-{{ endPage }}条</div>
      </div>
      <div class="pagination-block">
        <el-pagination
          prev-text="上一页"
          next-text="下一页"
          :page-size="pageSize"
          layout="prev, pager, next, jumper"
          :total="total"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {static_aplace_info_freed, queryCommonXueqi, static_aplace_info_exportPlaceInfoFree} from "@/api/trainingManagement/place.js";
import ButtonItem from "@/components/ButtonItem.vue";

export default {
  components: {
    ButtonItem,
  },
  data() {
    return {
      total: 0, //总条数
      pageSize: 50, //条数
      startPage: 0, //开始条数
      endPage: 0, //结束条数
      currentPage: 1, //当前页数
      jsonFields: {
        场地场所名称: "cdmc",
        面积: "mj",
        容纳人数: "krnrs",
        场地场所类型: "cdlx",
        所属校区: "ssxq",
        所属建筑物: "ssjzw",
        管理部门: "dept",
        楼层: "floor"
      },
      semester: "", //学期
      semesterList: [], //学期列表
      weekList: [
        { label: "1", value: "1" },
        { label: "2", value: "2" },
        { label: "3", value: "3" },
        { label: "4", value: "4" },
        { label: "5", value: "5" },
        { label: "6", value: "6" },
        { label: "7", value: "7" },
        { label: "8", value: "8" },
        { label: "9", value: "9" },
        { label: "10", value: "10" },
        { label: "11", value: "11" },
        { label: "12", value: "12" },
        { label: "13", value: "13" },
        { label: "14", value: "14" },
        { label: "15", value: "15" },
        { label: "16", value: "16" },
      ],
      startWeek: "", //开始周次
      endWeek: "", //结束周次
      startDdate: "", //开始时间
      endDate: "", //结束时间
      startSection: "", //开始节次：
      endSection: "", //结束节次：
      sectionList: [
        { label: "1", value: "1" },
        { label: "2", value: "2" },
        { label: "3", value: "3" },
        { label: "4", value: "4" },
        { label: "5", value: "5" },
        { label: "6", value: "6" },
        { label: "7", value: "7" },
        { label: "8", value: "8" },
        { label: "9", value: "9" },
        { label: "10", value: "10" }], //节次列表
      queryWeekList: [], //星期查询
      // 选择列表数据
      multipleSelection: [],
      query_date: true, //查询方式
      query_week: "", //
      tableData: [],
    };
  },
  created() {},
  mounted() {
    this.queryCommonXueqi();
  },
  methods: {
    exportData() {
      let min = parseInt(this.startSection);
        let max = parseInt(this.endSection);
        if (min > max) {
          this.$message.warning("请选择正确的上课节次!");
          return;
        }
        let course = [];
        for (let i = min; i <= max; i++) {
          course.push(i);
        }
      var params = {};
      // 按时间查询
      if (this.query_date) {
        if ((this.startDdate == "" || this.startDdate == null) || (this.endDate == "" || this.endDate == null)) {
          this.$message.warning("请先选择开始时间和结束时间")
          return;
        }
        params = {
            "timeR": {
                "startTime": this.startDdate,
                "endTime": this.endDate
            },
            "courses":course,
            "week": this.queryWeekList
        }
      } else {
        if ((this.startWeek == "" || this.startWeek == null) || (this.endWeek == "" || this.endWeek == null)) {
          this.$message.warning("请先选择开始周次和结束周次!")
          return;
        }
        params = {
            "weekR": {
                "xueqi": this.semester,
                "startWeek": this.startWeek,
                "endWeek":this.endWeek
            },
            "courses":course,
            "week": this.queryWeekList
        }
      }
      static_aplace_info_exportPlaceInfoFree(params).then((res) => {
        /** 获取生成设置好的文件名 */
        /** 接收文件流 */
        const blob = new Blob([res.data]);
        let url = URL.createObjectURL(blob);
        /** 模拟浏览器操作Document，并模拟下载动作 */
        let link = document.createElement("a");
        link.style.display = "none";
        link.href = url;
        link.setAttribute("download", "空闲场地.xlsx");
        document.body.appendChild(link);
        link.click();
      });
    },
    // 跳转到
    toOpenPlace(row) {
      this.$router.push({
        path: "/admin/course/venueScheduleQuery",
        query: {
          placeId: row.cdmc
        }
      })
    },
    // 获取学期列表
    async queryCommonXueqi() {
      const res = await queryCommonXueqi();
      if (res.data.code == 200) {
        this.semesterList = res.data.data;
        this.semester = this.semesterList[0].id
      }
    },
    // 查询空闲场地列表
    async queryFreePlace() {
      let min = parseInt(this.startSection);
        let max = parseInt(this.endSection);
        if (min > max) {
          this.$message.warning("请选择正确的上课节次!");
          return;
        }
        let course = [];
        for (let i = min; i <= max; i++) {
          course.push(i);
        }
      var params = {};
      // 按时间查询
      if (this.query_date) {
        if ((this.startDdate == "" || this.startDdate == null) || (this.endDate == "" || this.endDate == null)) {
          this.$message.warning("请先选择开始时间和结束时间")
          return;
        }
        params = {
            "timeR": {
                "startTime": this.startDdate,
                "endTime": this.endDate
            },
            "courses":course,
            "week": this.queryWeekList
        }
      } else {
        if ((this.startWeek == "" || this.startWeek == null) || (this.endWeek == "" || this.endWeek == null)) {
          this.$message.warning("请先选择开始周次和结束周次!")
          return;
        }
        params = {
            "weekR": {
                "xueqi": this.semester,
                "startWeek": this.startWeek,
                "endWeek":this.endWeek
            },
            "courses":course,
            "week": this.queryWeekList
        }
      }
      var current = this.currentPage;
      var size = this.pageSize;
      const res = await static_aplace_info_freed(params, current, size);
      if (res.data.code == 200) {
        this.tableData = res.data.data.records;
        this.total = res.data.data.total;
        var length = res.data.data.records.length;
        this.calculatePage(this.pageSize, res.data.data.current, length);
      }
    },
    // 计算开始和结束条数
    calculatePage(pageSize, current, length) {
      if (current == 1) {
        if (length != 0) {
          this.startPage = 1;
          this.endPage = length;
        }
      } else {
        this.startPage = pageSize * (current - 1) + 1;
        this.endPage = parseInt(this.startPage) + parseInt(length) - 1;
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.queryFreePlace();
    },
    changeQueryType() {
      if (this.query_date == true) {
        this.query_week = false;
      } else {
        this.query_week = true;
      }
    },
    changeQueryWeek() {
      if (this.query_week == true) {
        this.query_date = false;
      } else {
        this.query_date = true;
      }
    },
    // 表格选择事件
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    addUsedInfo() {
      this.$router.push({
        path: "/admin/place/addUsedRegister",
        params: {
          type: "add",
        },
      });
    },
    editUsedInfo(row) {
      this.$router.push({
        path: "/admin/place/addUsedRegister",
        params: {
          type: "edit",
          row: row,
        },
      });
    },
  },
  computed: {}, //计算属性
};
</script>
<style lang="less" scoped>
.assessment-container {
  .el-dialog__body {
    height: 251px;
  }
  .el-dialog__footer {
    padding: 10px 20px 45px;
  }
  .el-checkbox__inner:hover {
    border-color: #004ca7;
  }
  .el-checkbox__inner {
    border: 1px solid #004ca7;
    border-radius: 0;
    height: 18px;
    width: 18px;
  }
  .el-checkbox__inner::after {
    height: 10px;
    left: 6px;
  }
  .el-checkbox__input.is-checked .el-checkbox__inner,
  .el-checkbox__input.is-indeterminate .el-checkbox__inner {
    background-color: #004ca7;
    border-color: #004ca7;
  }
  .el-checkbox__input.is-checked + .el-checkbox__label {
    color: #004ca7;
  }
  .el-checkbox.is-bordered.is-checked {
    border-color: #004ca7;
  }
  .el-checkbox__input.is-focus .el-checkbox__inner {
    border-color: #004ca7;
  }
}
</style>
<style lang="less" scoped>
@import "../../../../styles/text.less";
/deep/ .szjky .el-dialog__body {
  height: 432px;
}
.pagination-block {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  justify-content: center;
}
</style>
<style lang="less" scoped>
@import "../../../../styles/supervision.less";

// 表格底部样式
@import "../../../../styles/delayedExam.less";

.wrapper-container {
  width: calc(100% - 20px);
  margin: 0 auto;
}

/deep/ .table-container {
  .el-table td,
  .el-table th.is-leaf,
  .el-table--border,
  .el-table--group {
    border-color: #c7d7eb;
  }

  .el-table__header tr,
  .el-table__header th {
    padding: 0;
    height: 76px;
    background-color: #edf2f9;
    font-size: 16px;
    font-family: Microsoft YaHei;
    font-weight: bold;
    line-height: 20px;
    color: #004ca7;
  }

  .el-table__body tr,
  .el-table__body td {
    padding: 0;
    height: 86px;
  }

  .wrapper-button {
    display: flex;
    flex-direction: column;
    align-items: center;

    > div {
      margin-bottom: 8px;
    }
  }
  .table-data {
    width: 100%;
    border: 0.8px solid #004ca7;
    /*border-radius: 9px;*/
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #004ca7;
  }
}

.wrapper-container .search-container .search-container-left {
  flex-wrap: wrap;
  .search-container-item {
    width: 12%;
    margin-right: 0px;
    .box {
      > span {
        text-align: left;
      }
      display: flex;
      justify-content: space-evenly;
      margin-bottom: 20px;
      align-items: center;
    }
  }
}

/deep/.el-date-editor.el-input {
  width: 110px;
}
/deep/ .el-input--medium {
  width: 110px;
  .el-input__inner {
    height: 28px;
    width: 110px;
    padding: 0px 18px;
  }
}
/deep/ .el-table thead {
  color: #004ca7 !important;
}

.btn {
  margin-top: 271px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 271px;
  .submit {
    width: 84px;
    height: 27px;
    line-height: 8px;
  }
}
.bottom-block {
  // display: flex;
  flex-direction: row;
  justify-content: space-between !important;
  .export-button {
    cursor: pointer;
    width: 127px;
    height: 27px;
    background: #fff6ef;
    border: 1px solid #f3b815;
    border-radius: 4px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    margin-top: 14px;
    > img {
      width: 17px;
      height: 17px;
    }
    > span {
      margin-left: 12px;
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #f3b815;
    }
  }
  .text {
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    line-height: 39px;
    color: #004ca7;
  }
}

.top-border {
  border: 1px solid #004ca7;
  padding: 20px;
  border-radius: 5px;

  .query-title {
    color: #004ca7;
    font-weight: bold;
  }
}

.query-date {
  /deep/ .el-input--medium .el-input__inner {
    // height: 36px !important;
    width: 180px;
  }

  /deep/ .el-date-editor .el-input__inner {
    height: 28px;
  }

  /deep/ .el-date-editor .el-input__prefix {
    top: -3px
  }
  .red {
    color: red;
  }
}
.search-button {
  width: 77px;
  height: 28px;
  line-height: 28px;
  background: #f1f8fe;
  border: 1px solid #51aef6;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  > span {
    font-size: 16px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #51aef6;
  }
  > img {
    width: 20px;
    height: 20px;
    margin-right: 4px;
  }
}

.mar-lef-2 {
        margin-left: 20px;
}
.undeLine {
    text-decoration:underline;
    cursor: pointer;
}


.assessment-container {
  margin: 10px;
  width: 100%;
  .table-container {
    margin-top: 20px;
    width: calc(100% - 20px);
  }
}

.lx-top-body {
  margin: 25px 10px;
  display: flex;
  justify-content: space-between;
  width: calc(100% - 20px);
}
</style>
